package com.hfad.todolistapp.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.hfad.todolistapp.model.TodoItem;

import java.util.LinkedList;
import java.util.List;

public class DataBaseManager {
    private static final String TAG = "DataBaseManager";

    private static DataBaseManager instance;
    private SqlHelper sqlHelper;
    private SQLiteDatabase dbWriter;
    private SQLiteDatabase dbReader;

    private DataBaseManager(Context context) {
        sqlHelper = new SqlHelper(context);
        dbReader = sqlHelper.getReadableDatabase();
        dbWriter = sqlHelper.getWritableDatabase();
    }

    public static DataBaseManager getInstance(Context context) {
        if (instance == null) {
            synchronized (DataBaseManager.class) {
                if (instance == null) {
                    instance = new DataBaseManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public boolean addTodoItem(TodoItem data) {
        return sqlHelper.addTodoItem(dbReader, data) >= 0;
    }

    public void deleteTodoItem(int id) {
        sqlHelper.deleteTodoItem(dbWriter, id);
    }

    public boolean updateTodoItem(TodoItem data) {
        return sqlHelper.updateTodoItem(dbWriter, data) >= 0;
    }

    public List<TodoItem> getTodoItemListOrderByCreatedTime(boolean isASC) {
        return sqlHelper.getTodoItemListOrderByCreatedTime(dbWriter, isASC);
    }

    public TodoItem getTodoItemById(int id) {
        return sqlHelper.getTodoItemById(dbWriter, id);
    }
}
